package com.zejson.controller;


import com.zejson.entity.TruckTeam;
import com.zejson.service.TruckTeamService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

@Controller
@Scope("prototype")
@RequestMapping("truck_team")
public class Truck_teamController {

    @RequestMapping("")
    public ModelAndView index()throws ClassNotFoundException{
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        TruckTeamService truckTeamService =  ((TruckTeamService)applicationContext.getBean("truckTeamService"));
        List teams = truckTeamService.getList();

        ModelAndView model = new ModelAndView("/templates/driver/truck_team");
        model.addObject("teams",teams);
        return model;
    }

    @RequestMapping("/add_truck_team.do")
    public String addTruckTeam(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, IllegalAccessException{
        ModelAndView model;
        if (request.getMethod().equals("POST")){
            String teanname = request.getParameter("TeamName");
            String leader = request.getParameter("Leader");
            String remark = request.getParameter("Remark");

            TruckTeam truckTeam = new TruckTeam();
            truckTeam.setTeamname(teanname);
            truckTeam.setLeader(leader);
            truckTeam.setRemark(remark);
            Date utilDate  =new Date();
            java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
            truckTeam.setCheckintime(sqlDate);
            truckTeam.setIsdeleted((byte) 0);

            System.out.println(truckTeam);

            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
            TruckTeamService truckTeamService = ((TruckTeamService)applicationContext.getBean("truckTeamService"));
            truckTeamService.insertByAutoGeneratePK("TruckTeamMapper",truckTeam);
        }
        return "redirect:/truck_team";
    }

    @RequestMapping("/update_truck_team")
    public String updateTruckTeam(HttpServletRequest request) throws ClassNotFoundException {

        Integer ttId = Integer.parseInt(request.getParameter("ttId"));
        String teanname = request.getParameter("TeamName");
        String leader = request.getParameter("Leader");
        String remark = request.getParameter("Remark");

        TruckTeam truckTeam = new TruckTeam();
        truckTeam.setTeamid(ttId);
        truckTeam.setTeamname(teanname);
        truckTeam.setLeader(leader);
        truckTeam.setRemark(remark);
        Date utilDate  =new Date();
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
        truckTeam.setCheckintime(sqlDate);
        truckTeam.setIsdeleted((byte) 0);

        System.out.println(truckTeam);

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        TruckTeamService truckTeamService = ((TruckTeamService)applicationContext.getBean("truckTeamService"));
        truckTeamService.updateById("TruckTeamMapper",truckTeam);

        return "redirect:/truck_team";
    }

    @RequestMapping("/delete_truck_team")
    public String deleteTruckTeam(HttpServletRequest request) throws ClassNotFoundException {

        int ttId = Integer.parseInt(request.getParameter("ttId"));
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        TruckTeamService truckTeamService = ((TruckTeamService)applicationContext.getBean("truckTeamService"));
        truckTeamService.deleteById("TruckTeamMapper", ttId);
        return "redirect:/truck_team";
    }
}
